A veces, si tenemos pocos datos no es necesario incluir una tabla dinámica. Para incluir una tabla estática, usamos una instrucción knitr::kable(dataframe) dentro del bloque de código R que dibuja la tabla:
| CURSO | TOTAL_HOMBRES | TOTAL_MUJERES |
|---|---|---|
| 2007-2008 | 22107 | 37041 |
| 2008-2009 | 21652 | 36047 |
| 2009-2010 | 21502 | 35007 |
| 2010-2011 | 20814 | 33868 |
| 2011-2012 | 21490 | 34415 |
| 2012-2013 | 24356 | 40511 |
| 2013-2014 | 23472 | 38470 |
| 2014-2015 | 22501 | 36426 |
| 2015-2016 | 22004 | 35784 |
| 2016-2017 | 21245 | 34127 |
| 2017-2018 | 20273 | 32849 |
| 2018-2019 | 19767 | 32445 |
| 2019-2020 | 19630 | 32533 |
| 2020-2021 | 19066 | 32314 |
| 2021-2022 | 17643 | 30628 |
| 2022-2023 | 17558 | 30491 |
Para las tablas dinámicas usando DT escribimos el siguiente bloque de código R:
Markdown permite introducir código HTML directamente. Para dibujar imágenes en el dashboard, escribimos el siguiente código HTML en la componente donde queramos dibujar la imagen.
---
title: "Alumnado UCM"
author: "Pedro de la Muela"
date: "`r Sys.Date()`"
output:
flexdashboard::flex_dashboard:
source_code: embed
logo: logos/woman.png
favicon: logo/ucm:favicon.png
social: [ "twitter", "facebook", "menu"]
navbar:
- { title: "Ref markdown basic", href: "https://www.markdownguide.org/basic-syntax/", align: left }
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(dplyr)
library(ggplot2)
library(DT)
library(plotly)
library(kableExtra)
df <- read_delim("datos_tratados.csv", delim = ";")
df$CURSO <- factor(df$CURSO)
df$CENTRO <- factor(df$CENTRO)
```
# Página 1
## Columna 1
### Tabla estática {data-height=300}
A veces, si tenemos pocos datos no es necesario incluir una tabla dinámica. Para incluir una tabla estática, usamos una instrucción **knitr::kable(dataframe)** dentro del bloque de código R que dibuja la tabla:
```r
grouped_data <- df %>%
group_by(CURSO) %>%
select(CURSO, TOTAL_HOMBRES, TOTAL_MUJERES) %>%
summarise(across(where(is.numeric), sum))
knitr::kable(grouped_data)
```
### Tabla estática {data-width=300}
```{r}
grouped_data <- df %>%
group_by(CURSO) %>%
select(CURSO, TOTAL_HOMBRES, TOTAL_MUJERES) %>%
summarise(across(where(is.numeric), sum))
knitr::kable(grouped_data) %>% kable_styling()
```
## Columna 2
### Tabla dinámica con DT {data-height=300}
Para las tablas dinámicas usando DT escribimos el siguiente bloque de código R:
```r
grouped_data <- df %>%
group_by(CURSO) %>%
select(CURSO, TOTAL_HOMBRES, TOTAL_MUJERES) %>%
summarise(across(where(is.numeric), sum))
DT::datatable(grouped_data, options = list( pageLength = 15))
```
[Sintaxis Markdown para incluir código R entre el texto](https://www.markdownguide.org/extended-syntax/#syntax-highlighting)
### Tabla dinámica
```{r}
grouped_data <- df %>%
group_by(CURSO) %>%
select(CURSO, TOTAL_HOMBRES, TOTAL_MUJERES) %>%
summarise(across(where(is.numeric), sum))
DT::datatable(grouped_data, options = list( pageLength = 15))
```
# Página 2
## Columna 1
### Imágenes dentro de un componente {data-height=300}
Markdown permite introducir código HTML directamente. Para **dibujar imágenes en el dashboard**, escribimos el siguiente **código HTML** en la componente donde queramos dibujar la imagen.
```r
<img src="./logos/ucm_favicon.png" width="100" >
```
### Resultado de dibujar una imagen
<img src="./logos/ucm_favicon.png" width="100" >
## Columna 2 {.tabset}
### Pestaña 1
```{r}
grouped_data <- df %>%
group_by(CURSO) %>%
summarise(across(where(is.numeric), sum))
df_4_M <- select(grouped_data,CURSO,TOTAL_MUJERES)
df_4_H <- select(grouped_data,CURSO,TOTAL_HOMBRES)
colnames(df_4_M)[2] <-"TOTAL"
colnames(df_4_H)[2] <-"TOTAL"
df_4_M$GÉNERO <- rep('Mujer', times = nrow(df_4_M))
df_4_H$GÉNERO <- rep('Hombre', times = nrow(df_4_H))
df_5 <- bind_rows(df_4_M,df_4_H)
datos_totales <- read_delim("datos_totales_largo.csv", delim = ";")
g1 <- ggplot(df_5, aes(x = CURSO, y = TOTAL, fill = GÉNERO)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Total de Alumnos",
x = "CURSO",
y = "Total") +
scale_fill_manual(values = c("Mujer" = "blue", "Hombre" = "red")) + theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust = 1, size = 8))
g2 <- ggplotly(g1)
g2
```
### Pestaña 2
```{r}
df_3 <- filter(df, CENTRO == "DERECHO" | CENTRO == "CIENCIAS_DE_LA_INFORMACION")
df_3$CURSO <- factor(df_3$CURSO)
df_3$CENTRO <- factor(df_3$CENTRO)
df_4_M <- select(df_3,CURSO,CENTRO,TOTAL_MUJERES)
df_4_H <- select(df_3,CURSO,CENTRO,TOTAL_HOMBRES)
colnames(df_4_M)[3] <-"TOTAL"
colnames(df_4_H)[3] <-"TOTAL"
df_4_M$GÉNERO <- rep('Mujer', times = nrow(df_4_M))
df_4_H$GÉNERO <- rep('Hombre', times = nrow(df_4_H))
# Ahora uno las tablas de Hombre y Mujer
df_5 <- bind_rows(df_4_M,df_4_H)
g1 <-ggplot(df_5) +
geom_boxplot(aes(y=TOTAL, x = CENTRO, fill = GÉNERO) ) +
labs(title = "Distribución de las mujeres y hombres que estudian en la UCM",
subtitle = "Gráficos de cajas, (geom_boxplot).",
x = " ",
y = "Número de estudiantes \n ",
caption = "Fuente: El Centro de Inteligencia Institucional, UCM."
) +
theme(axis.text.x = element_text(size = 10),
#arriba(top), izquierda (left), derecha (right)
legend.position = "top",
)
g1
```